/**
 * Created by ltx on 2017/6/1.
 */
function getStyle(obj, name)
{
    if(obj.currentStyle)
    {
        return obj.currentStyle[name];
    }
    else
    {
        return getComputedStyle(obj, false)[name];
    }
}

function startMove(obj, attr, iTarget)
{
    clearInterval(obj.timer);
    obj.timer=setInterval(function (){
        var cur=0;

        if(attr=='opacity')
        {
            cur=Math.round(parseFloat(getStyle(obj, attr))*100);
        }
        else
        {
            cur=parseInt(getStyle(obj, attr),10);
        }
       // console.log(getStyle(obj, attr));
        var speed=(iTarget-cur)/6;

        //console.log(speed);
        speed=speed>0?Math.ceil(speed):Math.floor(speed);
        //console.log(speed);
        if(cur==iTarget)
        {
            clearInterval(obj.timer);
        }
        else
        {
            if(attr=='opacity')
            {
                obj.style.filter='alpha(opacity:'+(cur+speed)+')';
                obj.style.opacity=(cur+speed)/100;

                // document.getElementById('txt1').value=obj.style.opacity;
            }
            else
            {
                obj.style[attr]=cur+speed+'px';
            }
        }
    }, 30);
}
function getByClass(oParent,sClass){
    var aEle=oParent.getElementsByTagName('*');
    var aResult=[];

    for(var i=0;i<aEle.length;i++){
        if(aEle[i].className==sClass){
            aResult.push(aEle[i]);
        }
    }
    return aResult;
}


var url="http://47.93.233.182/";

window.onload=function(){

    //获取头部电影
    getheadfilm();

    //获取电影
    getMovie();

    //获取短片
    getshortfiem();

    //获取tv作品数据
    gettvfilm();

    //获取广告数据
    getadvertise();

    //获取亮点数据
    getlightdata();

    //电影页弹出播放
    domask();

    //短片页弹出播放
    domask2();

    //首页电影弹出播放
    domaskhead();

    //广告弹出播放
    domaskadver();

}
//首页头部单独电影弹出播放
function domaskhead() {
    var mask1=$(".mask");
    var sidebar_trigger=$(".headshipinmask");

    sidebar_trigger.on('click',showSideBar);
    mask1.on('click',hideSideBar);


    function showSideBar() {
        mask1.fadeIn();
        sidebar_trigger.fadeOut();

        var video2=$(".headshipincontrol > embed");

        var videosize1=$(".videosize");
        video2.clone(true).appendTo(videosize1);

    }
    function hideSideBar() {
        mask1.fadeOut();
        sidebar_trigger.fadeIn();

        var video3=$(".videosize > embed");

        $('.videosize:eq(0)').html('');

        var shipin1=$(".headshipincontrol > embed");
        video3.appendTo(shipin1);
    }
}

//电影页弹出播放
function domask(){
    var mask1=$(".mask");
    var sidebar_trigger=$(".shipinmask");

    sidebar_trigger.on('click',showSideBar);
    mask1.on('click',hideSideBar);


    function showSideBar() {
        mask1.fadeIn();
        sidebar_trigger.fadeOut();

        var video2=$("#shipin > embed");

        var videosize1=$(".videosize");
        video2.clone(true).appendTo(videosize1);

    }
    function hideSideBar() {
        mask1.fadeOut();
        sidebar_trigger.fadeIn();

        var video3=$(".videosize > embed");

        $('.videosize:eq(0)').html('');

        var shipin1=$(".shipin > embed");
        video3.appendTo(shipin1);
    }
}

//短片页弹出播放
function domask2(){
    var mask1=$(".mask");
    var sidebar_trigger=$(".shipinmask2");

    sidebar_trigger.on('click',showSideBar);
    mask1.on('click',hideSideBar);


    function showSideBar() {
        mask1.fadeIn();
        sidebar_trigger.fadeOut();

        var video2=$("#shipin2 > video");

        var videosize1=$(".videosize");
        video2.clone(true).appendTo(videosize1);

    }
    function hideSideBar() {
        mask1.fadeOut();
        sidebar_trigger.fadeIn();

        var video3=$(".videosize > video");

        $('.videosize:eq(0)').html('');

        var shipin1=$(".shipin2 > video");
        video3.appendTo(shipin1);
    }
}

//广告弹出播放
function domaskadver() {
    var mask1=$(".mask");
    var sidebar_trigger=$(".shipinmask4");

    sidebar_trigger.on('click',showSideBar);
    mask1.on('click',hideSideBar);


    function showSideBar() {
        mask1.fadeIn();
        sidebar_trigger.fadeOut();

        var video2=$("#shipin4 > video");

        var videosize1=$(".videosize");
        video2.clone(true).appendTo(videosize1);

    }
    function hideSideBar() {
        mask1.fadeOut();
        sidebar_trigger.fadeIn();

        var video3=$(".videosize > video");

        $('.videosize:eq(0)').html('');

        var shipin1=$(".shipin4 > video");
        video3.appendTo(shipin1);
    }
}


/*电影小图轮播*/
function smallpicgo() {
    var oDiv=document.getElementsByClassName('movies-1')[0];
    var oBtnPre=getByClass(oDiv,'prev1')[0];
    var oBtnNext=getByClass(oDiv,'next1')[0];

    var oDivSmall=getByClass(oDiv,'control-ul-wid')[0];
    var oUlSmall=oDivSmall.getElementsByTagName('ul')[0];//$(".img-control ul").eq(0).width();
    var aLiSmall=oDivSmall.getElementsByTagName('li');

    var now=0;

    oUlSmall.style.width=aLiSmall.length*aLiSmall[0].offsetWidth+'px';
    // $(".img-control ul").eq(0).width(aLiSmall.length*aLiSmall[0].offsetWidth+'px');
    //console.log(oUlSmall.style.width);

    //图切换
    if (aLiSmall.length>=3){
        for(var i=0;i<aLiSmall.length;i++){
            aLiSmall[i].index=i;
            aLiSmall[i].onclick=function()
            {
                if(this.index==now) return;
                now=this.index;
                tab();
            }
        }
        oBtnPre.onclick=function(){
            now--;
            if(now==-1){
                now=aLiSmall.length-1;
            }
            tab();
        }
        oBtnNext.onclick=function(){
            now++;
            if(now==aLiSmall.length){
                now=0;
            }
            tab();
        }
        function tab(){
            if(now==0){
                startMove(oUlSmall,'left',0);
            }
            else if(now>aLiSmall.length-1 ){
                startMove(oUlSmall,'left',0)
            }
            else{
                startMove(oUlSmall,'left',-(now-1)*aLiSmall[0].offsetWidth);
               // console.log(-(now-1)*aLiSmall[0].offsetWidth);
            }
        }
    }
}

/*短片小图轮播*/
function shortlunbo() {
    var oDiv=document.getElementsByClassName('movies-2')[0];
    var oBtnPre=getByClass(oDiv,'prev2')[0];
    var oBtnNext=getByClass(oDiv,'next2')[0];

    var oDivSmall2=getByClass(oDiv,'movies-2-ul')[0];
    var oUlSmall2=oDivSmall2.getElementsByClassName('fanfan')[0];
    var aLiSmall2=oDivSmall2.getElementsByTagName('li');

  /*  var mmm=oDivSmall2.getElementsByClassName("oli1")[0];*/

    var now=0;

    oUlSmall2.style.width=aLiSmall2.length * 270 +'px';

    //图切换

    for(var i=0;i<aLiSmall2.length;i++){
        aLiSmall2[i].index=i;
        aLiSmall2[i].onclick=function()
        {
            if(this.index==now) return;
            now=this.index;
            tab();
        }
    }
    oBtnPre.onclick=function(){
        now--;
        if(now==-1){
            now=aLiSmall2.length-1;
        }

        tab();
    }
    oBtnNext.onclick=function(){
        now++;
        if(now==aLiSmall2.length){
            now=0;
        }
        tab();
    }
    function tab(){
        if(now==0){
            startMove(oUlSmall2,'left',0);
        }
        else if(now==aLiSmall2.length-1){
            startMove(oUlSmall2,'left',-(now-2)*270);
        }
        else{
            startMove(oUlSmall2,'left',-(now-1)*270);
        }
    }
}

//亮点轮播
function lightlunbo() {
    var oDiv=document.getElementsByClassName('light-1')[0];
    var oBtnPre=getByClass(oDiv,'prev3')[0];
    var oBtnNext=getByClass(oDiv,'next3')[0];

    var oDivSmall2=getByClass(oDiv,'light-control')[0];
    var oUlSmall2=oDivSmall2.getElementsByClassName('fanfanfan')[0];
    var aLiSmall2=oDivSmall2.getElementsByTagName('li');

    /*  var mmm=oDivSmall2.getElementsByClassName("oli1")[0];*/

    var now=0;

    for (var i = aLiSmall2.length; i--;) {
        aLiSmall2[i].style.width = 482+ 'px'
    }
    oUlSmall2.style.width=aLiSmall2.length * aLiSmall2[0].offsetWidth + 4 +'px';
/*    console.log(oUlSmall2.style.width);*/

    //图切换

    oBtnPre.onclick=function(){
        now--;
        if(now==-1){
            now=aLiSmall2.length-1;
        }

        tab();
    }
    oBtnNext.onclick=function(){
        now++;
        if(now==aLiSmall2.length){
            now=0;
        }

        tab();
    }
    function tab(){

        if(now==0){
            startMove(oUlSmall2,'left',0);
        }
        else if(now==aLiSmall2.length-1){
            startMove(oUlSmall2,'left',-(now-2)*aLiSmall2[0].offsetWidth);
        }
        else{
            startMove(oUlSmall2,'left',-(now-1)*aLiSmall2[0].offsetWidth);
        }
    }
}

//获取头部视屏数据
function getheadfilm() {
    $.ajax({
        url:url+"/getHeadMovie",
        type:'GET',
        dataType:'JSON',
        success:function (text) {

            //创建头部视频部分
            headshipin(text);
     /*       console.log(text);*/
        },
        error:function () {
            alert('wrong');
        }
    });
}

//请求电影数据
function getMovie() {
    $.ajax({
        url:url+"/getMovie",
        type:'GET',
        dataType:'JSON',
        success:function (text) {
            //动态创建li
            movieList(text);
        /*    console.log(text);*/
        },
        error:function () {
            alert('wrong');
        }
    });
}

//请求短片数据
function getshortfiem() {
    $.ajax({
        url:url+"/getShortFilm",
        type:'GET',
        dataType:'JSON',
        success:function (text) {

            //动态创建短片
            duanpian(text);
       /*     console.log(text);*/
        },
        error:function () {
            alert('wrong');
        }
    });
}

//获取tv数据
function gettvfilm() {
    $.ajax({
        url:url+"/getTv",
        type:'GET',
        dataType:'JSON',
        success:function (text) {

            //动态创建tv
            chuangjiantv(text);
          // console.log(text);
        },
        error:function () {
            alert('wrong');
        }
    });
}


//请求亮点数据
function getlightdata() {
    $.ajax({
        url:url+"/getHeighLightList",
        type:'GET',
        dataType:'JSON',
        success:function (text) {

            //动态创亮点
            lightlist(text);

        },
        error:function () {
            alert('wrong');
        }
    });
}

//tv部分创建
function chuangjiantv(text){
    var tvlunbo=document.getElementsByClassName("tvlun")[0];
    for (var i=0;i<text.data.length;i++) {
        var tvdiv=document.createElement("div");

        if (i ==0){
            tvdiv.setAttribute("class","item active first-one");
        }
        else{
            tvdiv.setAttribute("class","item first-one");
        }
        tvlunbo.appendChild(tvdiv);

        var father2=document.getElementsByClassName("item first-one")[i];

        var tvshipin=document.createElement("div");
        father2.appendChild(tvshipin);
        tvshipin.setAttribute("class","tvshipin");

        var tvvideo=document.createElement("video");
        tvvideo.setAttribute("class","tvvdioo");
        tvshipin.appendChild(tvvideo);

        var carouseldiv=document.createElement("div");
        carouseldiv.setAttribute("class","carousel-caption");
        father2.appendChild(carouseldiv);

        var father3=document.getElementsByClassName("carousel-caption")[i];
        var movnumdiv=document.createElement("div");
        movnumdiv.setAttribute("class","mov-number");
        father3.appendChild(movnumdiv);
        var movnum_a=document.createElement("a");
        movnumdiv.appendChild(movnum_a);
        movnum_a.setAttribute("class","movnuma");
        var ashuju=text.data[i].dijiji;
        $(".movnuma").eq(i).html(ashuju);

        var movimgdiv=document.createElement("div");
        movimgdiv.setAttribute("class","mov-img");
        father3.appendChild(movimgdiv);
        var movimg_img=document.createElement("img");
        movimg_img.setAttribute("class","movimg-img");
        movimgdiv.appendChild(movimg_img);
        var movimg_img2=document.createElement("img");
        movimg_img2.setAttribute("class","movimg-img2");
        movimgdiv.appendChild(movimg_img2);

        var movimg_img3=document.createElement("img");
        movimg_img3.setAttribute("class","movimg-img3");
        movimgdiv.appendChild(movimg_img3);

        var movword=document.createElement("div");
        movword.setAttribute("class","mov-word");
        father3.appendChild(movword);
        var movword_h2=document.createElement("h2");
        movword.appendChild(movword_h2);
        var movword_p=document.createElement("p");
        movword.appendChild(movword_p);

        var shipinsrc=text.data[i].vedio_id;
        $(".tvshipin").eq(i).html(shipinsrc);

        var bg4=text.data[i].movie_image.split("+");
        $(".movimg-img").eq(i).attr("src",url+"/"+bg4[0]);
        $(".movimg-img2").eq(i).attr("src",url+"/"+bg4[1]);
        $(".movimg-img3").eq(i).attr("src",url+"/"+bg4[2]);

        /*//标题*/
        var hhhh=text.data[i].movie_name;
        $(".mov-word>h2").eq(i).html(hhhh);
/*文字*/
        var ppp=text.data[i].movie_introduce;
        $(".mov-word>p").eq(i).html(ppp);

    }
}

//亮点li创建
function lightlist(text) {
    var fanfanfan=document.getElementsByClassName("fanfanfan")[0];
    for(var m=0;m<text.data.length;m++) {

        var oli=document.createElement("li");
        oli.setAttribute("class","oli3");
        fanfanfan.appendChild(oli);
        (function () {
            var z=m;
            document.getElementsByClassName("oli3")[z].onclick=function () {
                window.location.href="light.html?aboutcompany_id="+text.data[z].aboutcompany_id;
            }
        })()
    }

    for (var i=0;i<text.data.length;i++){
        var span3=document.createElement("span");
       var h3=document.createElement("h3");
        var p3=document.createElement("p");
        var img3=document.createElement("img");
        var a3=document.createElement("a");
        h3.appendChild(a3);

        span3.setAttribute("class","span3-biaoti");

        var father3=document.getElementsByClassName("oli3")[i];
        img3.setAttribute("class","smallimg3");
        p3.setAttribute("class","p3_word");
        a3.setAttribute("class","lighth3");


        father3.appendChild(span3);
        father3.appendChild(h3);
        father3.appendChild(p3);
        father3.appendChild(img3);


       // var span3word=text.data[i].aboutcompany_id;
        if (i<10){
            $(".span3-biaoti").eq(i).html("0"+ (i+1));
        }
        else{
            $(".span3-biaoti").eq(i).html(i+1);
        }


        var h333=text.data[i].title;
        $(".lighth3").eq(i).html(h333);
        $(".lighth3").eq(i).attr("href","light.html");

        var ppppp=text.data[i].other;
        $(".p3_word").eq(i).html(ppppp);

        var image=text.data[i].aboutcompany_image.split("+")
        $(".smallimg3").eq(i).attr("src",url+"/"+image[0]);

    }
    lightlunbo();
}

//电影作品li创建
function movieList(text) {
    var fan=document.getElementsByClassName("fan")[0];
    for(var m=0;m<text.data.length;m++){

        var oli=document.createElement("li");
        oli.setAttribute("class","oli1");
        fan.appendChild(oli);

    }
    for(var i=0;i<text.data.length;i++){
        var img3=document.createElement("img");
        var p2=document.createElement("p");
        p2.setAttribute("class","biaoti");

        var father1=document.getElementsByClassName("oli1")[i];
        img3.setAttribute("class","smallimg");
        father1.appendChild(img3);
        father1.appendChild(p2);

        var image=text.data[i].movie_image.split("+")
        $(".smallimg").eq(i).attr("src",url+"/"+image[0]);

        var pp1=text.data[i].movie_name;
        $(".biaoti").eq(i).html(pp1);

        if(i==0){
            var bg4=text.data[i].firstimg.split("+");
            var src4=text.data[i].vedio_id;
            $("#shipin").eq(0).html(src4);
            $(".movies-1-bg").eq(0).attr("src",url+"/"+bg4[0]);

            var firstposter=text.data[i].video_poster.split("+");
            $(".movies1Poster > img").eq(0).attr("src",url+"/"+firstposter[0]);
        }

        (function () {
            var b=i;
            document.getElementsByClassName("smallimg")[b].onclick=function () {
                /*console.log(b);*/
                var bg1=text.data[b].firstimg.split("+");
                var src=text.data[b].vedio_id;
                $("#shipin").eq(0).html(src);
                $(".movies-1-bg").eq(0).attr("src",url+"/"+bg1[0]);

                var firstposter2=text.data[b].video_poster.split("+");
                $(".movies1Poster > img").eq(0).attr("src",url+"/"+firstposter2[0]);
            }
        })()
    }
    /*小图轮播*/
    smallpicgo();
}

//短片作品li创建
function duanpian(text) {
    var fanfan=document.getElementsByClassName("fanfan")[0];
    for(var m=0;m<text.data.length;m++) {

        var oli=document.createElement("li");
        oli.setAttribute("class","oli2");
        fanfan.appendChild(oli);
    }

    for (var i=0;i<text.data.length;i++){
        var div2=document.createElement("div");
        var img2=document.createElement("img");
        var p2=document.createElement("p");
        var span2=document.createElement("span");

        span2.setAttribute("class","span2-biaoti");

        var father1=document.getElementsByClassName("oli2")[i];
        img2.setAttribute("class","smallimg2");
        p2.setAttribute("class","p2_word");
        father1.appendChild(div2);
        div2.appendChild(img2);
        father1.appendChild(span2);
        father1.appendChild(p2);


        var image=text.data[i].movie_image.split("+");
        $(".smallimg2").eq(i).attr("src",url+"/"+image[0]);
        var ppppp=text.data[i].movie_introduce;
        $(".p2_word").eq(i).html(ppppp);

        var span2word=text.data[i].movie_name;
        $(".span2-biaoti").eq(i).html(span2word);

        if(i==0){
            var bg2=text.data[i].movie_image.split("+");
            var src2=text.data[i].vedio_id;

            $(".movies-2-bg").eq(0).attr("src",url+"/"+bg2[0]);
            $("#shipin2video").eq(0).attr("src",src2);
            var poster=text.data[i].video_poster.split("+");
            $(".shortposter > img").eq(0).attr("src",url+"/"+poster[0]);
        }
        (function () {
            var b=i;
            document.getElementsByClassName("smallimg2")[b].onclick=function () {
                var bg3=text.data[b].movie_image.split("+");
                var src=text.data[b].vedio_id;

/*                $("#shipin2").eq(0).html(src);*/
                $(".movies-2-bg").eq(0).attr("src",url+"/"+bg3[0]);
                $("#shipin2video").eq(0).attr("src",src);

                var poster2=text.data[b].video_poster.split("+");
                $(".shortposter > img").eq(0).attr("src",url+"/"+poster2[0]);
            }
        })()
    }
    shortlunbo();
}

//头部视频部分创建
function headshipin(text) {
//获取视频
    var src4=text.data[0].vedio_id;
    $(".headshipincontrol").eq(0).html(src4);

    var firsttvp=document.getElementsByClassName("first-tv-p")[0];

    var shipinp=document.createElement("p");
    shipinp.setAttribute("class","shipinp1");
    firsttvp.appendChild(shipinp);

    var pppppp=text.data[0].movie_introduce;
    $(".shipinp1").eq(0).html(pppppp);
    var poster=text.data[0].video_poster.split("+");
    console.log(poster);
    $(".headposter > img").eq(0).attr("src",url+"/"+poster[0]);
}

//请求广告数据
function getadvertise() {
    $.ajax({
        url:url+"/getAd",
        type:'GET',
        dataType:'JSON',
        success:function (text) {

            //动态创建短片
            adver(text);
                // console.log(text);
        },
        error:function () {
            alert('wrong');
        }
    });
}

//广告作品动态创建
function adver(text) {
    var fanfan4=document.getElementsByClassName("fanfan4")[0];
    for(var m=0;m<text.data.length;m++) {

        var oli=document.createElement("li");
        oli.setAttribute("class","oli4");
        fanfan4.appendChild(oli);
    }

    for (var i=0;i<text.data.length;i++){
        var div2=document.createElement("div");
        var img2=document.createElement("img");
        var div4=document.createElement("div");
        var p2=document.createElement("p");
        var span2=document.createElement("span");

        span2.setAttribute("class","span4-biaoti");
        p2.setAttribute("class","p4_word");
        div4.setAttribute("class","divmask")

        var father1=document.getElementsByClassName("oli4")[i];
        img2.setAttribute("class","smallimg4");

        father1.appendChild(div2);
        div2.appendChild(img2);
        div2.appendChild(div4);
        div4.appendChild(span2);
        div4.appendChild(p2);
        var ppppp=text.data[i].movie_introduce;
        $(".p4_word").eq(i).html(ppppp);
        var span2word=text.data[i].movie_name;
        $(".span4-biaoti").eq(i).html(span2word);

        var image=text.data[i].movie_image.split("+")
        $(".smallimg4").eq(i).attr("src",url+"/"+image[0]);

        if(i==0){
            var bg2=text.data[i].movie_image.split("+");
            var src2=text.data[i].vedio_id;

            $(".movies-4-bg").eq(0).attr("src",url+"/"+bg2[0]);
            /*    $("#shipin2").eq(0).html(src2);*/
            $("#shipin4video").eq(0).attr("src",src2);

            var poster=text.data[i].video_poster.split("+");
            $(".adposter > img").eq(0).attr("src",url+"/"+poster[0]);
        }
        (function () {
            var b=i;
            document.getElementsByClassName("divmask")[b].onclick=function () {
                var bg3=text.data[b].movie_image.split("+");
                var src=text.data[b].vedio_id;
                /* $("#shipin2").eq(0).html(src);*/
                $(".movies-4-bg").eq(0).attr("src",url+"/"+bg3[0]);
                $("#shipin4video").eq(0).attr("src",src);
                var poster2=text.data[b].video_poster.split("+");
                $(".adposter > img").eq(0).attr("src",url+"/"+poster2[0]);
                if (b%2==1){
                    $(".movies-4-control").css({"background":"#000"});
                }
                else {
                    $(".movies-4-control").css({"background":"#35015a"});
                }
            }
        })()
    }
    adverlunbo();
}

//广告轮播
function adverlunbo() {
    var oDiv=document.getElementsByClassName('movies-4')[0];
    var oBtnPre=getByClass(oDiv,'prev4')[0];
    var oBtnNext=getByClass(oDiv,'next4')[0];

    var oDivSmall2=getByClass(oDiv,'movies-4-ul')[0];
    var oUlSmall2=oDivSmall2.getElementsByClassName('fanfan4')[0];
    var aLiSmall2=oDivSmall2.getElementsByTagName('li');

    var now=0;

    oUlSmall2.style.width=aLiSmall2.length * 270 +'px';

    //图切换
    for(var i=0;i<aLiSmall2.length;i++){
        aLiSmall2[i].index=i;
        aLiSmall2[i].onclick=function()
        {
            if(this.index==now) return;
            now=this.index;
            tab();
        }
    }
    oBtnPre.onclick=function(){
        now--;
        if(now==-1){
            now=aLiSmall2.length-1;
        }

        tab();
    }
    oBtnNext.onclick=function(){
        now++;
        if(now==aLiSmall2.length){
            now=0;
        }

        tab();
    }
    function tab(){

        if(now==0){
            startMove(oUlSmall2,'left',0);
        }
        else if(now==aLiSmall2.length-1){
            startMove(oUlSmall2,'left',-(now-2)*270);
        }
        else{
            startMove(oUlSmall2,'left',-(now-1)*270);
        }
    }
}